import { VIEWCONTENT_TYPE_CODE } from "@/utils/viewtype";
import { getPathListAsync } from "../folder";

const config_3d = {
  type: "3D",
  annoAble: true, // 支持标注
  fullscreen: true, // 支持全屏
  viewContent: VIEWCONTENT_TYPE_CODE.THREE_3D, //资源对应的视图
  // title: "detailInfo.file.threeDComm",
  modelControl: true,
  getViewObj: item => {
    const { EXTENTION } = item;
    let srcExt = "";
    const extension = EXTENTION && EXTENTION.toUpperCase();
    if (extension === "HDR" || extension === "EXR") {
      let lurl = item.L_URL;
      item.M_URL = lurl;
      srcExt = lurl.substr(lurl.lastIndexOf(".") + 1).toLowerCase();
    } else {
      let murl = item.M_URL;
      srcExt = murl.substr(murl.lastIndexOf(".") + 1).toLowerCase();
    }
    return {
      ...item,
      url: item.M_URL, //"/static/modelSamples/models/DamagedHelmet.glb",
      //mtl: mtlData && urlConverter.urlJoint(mtlData.VALUE),
      assetInfo: {
        assetSeq: item.ASSET_SEQ,
        version: item.VER_NO,
        assetType: item.ASSET_TYPE,
        ext: item.EXTENSION,
        srcExt,
        permission: item.PERMIT_CODE
      }
    };
  }
};
const assetConfigs = {
  I: {
    type: "I",
    // title: "detailInfo.file.comm",
    isPoint: true, // 支持评论点
    annoAble: true, // 支持标注
    fullscreen: true, // 支持全屏
    viewContent: VIEWCONTENT_TYPE_CODE.IMG //资源对应的视图
  },
  FM: {
    type: "FM",
    // title: "detailInfo.file.comm",
    isPoint: true, // 支持评论点
    annoAble: true, // 支持标注
    fullscreen: true, // 支持全屏
    viewContent: VIEWCONTENT_TYPE_CODE.IMG //资源对应的视图
  },
  /** PSD切图 */
  C: {
    type: "C",
    isComment: false, // 默认为true
    fullscreen: false,
    fileDetailTitle: ["info"]
  },
  DWG: {
    assetType: "I",
    type: "DWG",
    annoAble: true, // 支持标注
    fullscreen: true, // 支持全屏
    viewContent: VIEWCONTENT_TYPE_CODE.CAD, //资源对应的视图
    getViewObj: item => {
      return {
        ASSET_SEQ: item.ASSET_SEQ,
        VER_NO: item.VER_NO,
        EXTENTION: item.EXTENTION,
        ASSET_TYPE: item.ASSET_TYPE,
        url: item.L_URL,
        M_URL: item.M_URL,
        isProcessing: item.isProcessing,
        state3d: item.state3d,
        assetInfo: {
          assetSeq: item.ASSET_SEQ,
          version: item.VER_NO,
          assetType: item.ASSET_TYPE,
          ext: "dwg",
          permission: item.PERMIT_CODE
        },
        watermark: item.watermark,
        isCurrentVer: item.isCurrentVer
      };
    }
  },
  GIF: {
    assetType: "I",
    type: "GIF",
    annoAble: true, // 支持标注
    fullscreen: true, // 支持全屏
    viewContent: VIEWCONTENT_TYPE_CODE.GIF //资源对应的视图
    // title: "detailInfo.file.gifComm"
  },
  V: {
    type: "V",
    annoAble: true, // 支持标注
    fullscreen: true, // 支持全屏
    viewContent: VIEWCONTENT_TYPE_CODE.VIDEO, //资源对应的视图
    icon: "el-icon-film",
    getViewObj: item => {
      return {
        ASSET_SEQ: item.ASSET_SEQ,
        VER_NO: item.VER_NO,
        EXTENTION: item.EXTENTION,
        ASSET_TYPE: item.ASSET_TYPE,
        isProcessing: item.isProcessing,
        state3d: item.state3d,
        pic: item.M_URL,
        url: item.L_URL,
        permitCode: item.PERMIT_CODE,
        watermark: item.watermark
      };
    }
    // title: "detailInfo.file.vComm"
  },
  "3D": config_3d,
  HDR: {
    ...config_3d,
    assetType: "3D",
    type: "HDR",
    annoAble: true, // 支持标注
    modelControl: false
  },
  SP: {
    type: "SP",
    annoAble: true, // 支持标注
    fullscreen: true, // 支持全屏
    viewContent: VIEWCONTENT_TYPE_CODE.SPINE, //资源对应的视图
    // title: "detailInfo.file.spComm",
    getViewObj: item => {
      return {
        ASSET_SEQ: item.ASSET_SEQ,
        VER_NO: item.VER_NO,
        EXTENTION: item.EXTENTION,
        ASSET_TYPE: item.ASSET_TYPE,
        isProcessing: item.isProcessing,
        state3d: item.state3d,
        url: item.M_URL,
        id: parseInt(item.ASSET_SEQ),
        version: item.VER_NO,
        watermark: item.watermark,
        assetInfo: {
          assetSeq: item.ASSET_SEQ,
          version: item.VER_NO,
          assetType: item.ASSET_TYPE,
          permitCode: item.PERMIT_CODE
        }
      };
    }
  },
  XD: {
    type: "XD",
    fullscreen: true, // 支持全屏
    isEnclosure: false, //不支持附件预览
    viewContent: VIEWCONTENT_TYPE_CODE.XD //资源对应的视图
    // title: "detailInfo.file.gifComm"
  },
  F1: {
    type: "F1",
    fullscreen: true, // 支持全屏
    viewContent: VIEWCONTENT_TYPE_CODE.OFFICE, //资源对应的视图
    defaultShowComment: false // 默认不显示评论
  },
  AI: {
    assetType: "F1",
    type: "AI",
    fullscreen: true, // 支持全屏
    viewContent: VIEWCONTENT_TYPE_CODE.OFFICE //资源对应的视图,
  },
  A: {
    type: "A",
    fullscreen: true, // 支持全屏
    viewContent: VIEWCONTENT_TYPE_CODE.AUDIO, //资源对应的视图
    getViewObj: (item, list, isViewVersion) => {
      return {
        ASSET_SEQ: item.ASSET_SEQ,
        VER_NO: item.VER_NO,
        EXTENTION: item.EXTENTION,
        ASSET_TYPE: item.ASSET_TYPE,
        isProcessing: item.isProcessing,
        state3d: item.state3d,
        url: item.L_URL,
        list,
        itemIndex: item.index,
        isViewVersion
      };
    }
  },
  O: {
    type: "O",
    fullscreen: false,
    viewContent: VIEWCONTENT_TYPE_CODE.OTHER //资源对应的视图
    // title: "detailInfo.file.gifComm"
  },
  XMIND: {
    assetType: "O",
    type: "XMIND",
    annoAble: false, // 支持标注
    fullscreen: true, // 支持全屏
    viewContent: VIEWCONTENT_TYPE_CODE.XMIND //资源对应的视图
  },
  /** 进入详情页还没有加载资源时，使用空状态，关闭所有功能 */
  _EMPTY: {
    type: "_EMPTY",
    fullscreen: false,
    isComment: false, // 默认为true
    viewContent: VIEWCONTENT_TYPE_CODE.OTHER //资源对应的视图
    // title: "detailInfo.file.gifComm"
  },
  DAE: {
    type: "DEA",
    // title: "detailInfo.file.comm",
    isPoint: true, // 支持评论点
    annoAble: true, // 支持标注
    fullscreen: true, // 支持全屏
    viewContent: VIEWCONTENT_TYPE_CODE.DEA, //资源对应的视图
    getViewObj: item => {
      return {
        ASSET_SEQ: item.ASSET_SEQ,
        VER_NO: item.VER_NO,
        EXTENTION: item.EXTENTION,
        ASSET_TYPE: item.ASSET_TYPE,
        isProcessing: item.isProcessing,
        state3d: item.state3d,
        url: item.M_URL,
        id: parseInt(item.ASSET_SEQ),
        version: item.VER_NO,
        watermark: item.watermark,
        assetInfo: {
          assetSeq: item.ASSET_SEQ,
          version: item.VER_NO,
          assetType: item.ASSET_TYPE,
          permitCode: item.PERMIT_CODE
        }
      };
    }
  }
};

/** 获取当前资源类型对应的评论配置 */
export const getAssetConfig = assetInfo => {
  if (!assetInfo) {
    return assetConfigs["_EMPTY"];
  }
  const config = assetConfigs[getAssetType(assetInfo)];
  if (config && config.isEnclosure !== false) {
    return config;
  }
  return assetConfigs["O"];
};

/** 根据 后缀 和 assetType 获取资源类型 */
export const getAssetType = assetInfo => {
  // 如果后缀有单独配置，根据 ASSET_TYPE 和 后缀 双主键匹配
  if (assetInfo.EXTENTION) {
    const key = assetInfo.EXTENTION.toUpperCase();
    const config = assetConfigs[key];
    if (config && config.assetType === assetInfo.ASSET_TYPE) {
      return key;
    }
  }
  return assetInfo.ASSET_TYPE;
};

const assetTypeExtensions = {
  GIF: ["gif"],
  AI: ["ai"],
  I: [
    "bmp",
    "gif",
    "heic",
    "ico",
    "jpeg",
    "jpg",
    "png",
    "svg",
    "eps",
    // "psd",
    "tif",
    "tiff",
    "webp",
    "icns",
    "arw",
    "cr2",
    "nef",
    "orf",
    "raf",
    "raw",
    "rw2",
    "dds",
    "exr",
    // "tga",
    "ttf",
    "3fr",
    "crw",
    "dng",
    "erf",
    "mrw",
    "nrw",
    "otf",
    "pef",
    "sr2",
    "srw",
    "x3f"
  ],
  V: ["mp4", "mkv", "mov", "webm", "mpg", "mpeg", "avi"],
  "3D": ["gltf", "glb", "hdr", "exr"],
  A: ["sculpt", "flac", "m4a", "mp3", "ogg", "wav"],
  XD: ["xd"],
  F1: [
    "pdf",
    "odt",
    "dotx",
    "ott",
    "rtf",
    "html",
    "fb2",
    "epub",
    //excel
    "xls",
    "xlsx",
    "xlsb",
    "xlsm",
    "xlst",
    //word
    "doc",
    "docx",
    "txt",
    //ppt
    "ppt",
    "pptx",
    "potx"
  ]
};

/* 后缀对应的资源类型 */
const extensionToAssetType = {};
Object.entries(assetTypeExtensions).forEach(([assetType, extensionArray]) => {
  extensionArray.forEach(extension => {
    extensionToAssetType[extension] = assetType;
  });
});

/* 根据后缀获取资源类型 */
export const getAssetTypeByExt = ext => {
  ext = ext.replace(".", "");
  return extensionToAssetType[ext.toLowerCase()] || "O";
};

/**
 * 如果资源路径不存在，设置资源路径
 * 防止重复加载
 * @param {*} asset
 */
export const setAssetPathListAsync = async asset => {
  if (!asset.pathList) {
    asset.pathList = await getPathListAsync({
      ASSET_LABEL_SEQ:
        asset.folderId || asset.FOLDER_ID || asset.ASSET_LABEL_SEQ // 搜索是FOLDER_ID，列表是folderId
    });
  }
  return asset.pathList;
};
